<template>
	<div>
		<van-action-bar>
			<van-action-bar-icon icon="chat-o" text="客服" dot />
			<van-action-bar-icon icon="cart-o" text="购物车" badge="5" />
			<van-action-bar-icon icon="shop-o" text="店铺" badge="12" />
			<van-action-bar-button
				type="warning"
				text="加入购物车"
				@click="addCommodity"
			/>
			<van-action-bar-button
				type="danger"
				text="立即购买"
				@click="gotoCart"
			/>
		</van-action-bar>
	</div>
</template>
<script setup>
import "vant/es/toast/style"
import { useRoute, useRouter } from "vue-router"
import { showSuccessToast, showToast } from "vant"

import { addCart } from "../services/index"

const route = useRoute()
const router = useRouter()

const proid = route.params.id

// 点击加入购物车
const addCommodity = async () => {
	const { data } = await addCart(proid, 1)

	if (data.code === "200") {
		showSuccessToast("添加成功！商品正在购物车等你")
	} else if (data.code === '10119'){
		showToast('未登录！请先登录')
		router.push('/login')
	}
	console.log(data)
}

// 点击立即购买 添加并跳转到购物车
const gotoCart = async () => {
	const { data } = await addCart(proid, 1)

	if (data.code === "200") {
		showSuccessToast("已为您添加至购物车")
	}
	console.log(data)

	router.push("/car")
}
</script>
